﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

using System.Data.SQLite;

namespace Classes
{
   public class EquipmentList
   {   
      #region Private Member Variables

      static private List<Equipment> list = null;

      #endregion
      
      #region Setup Functions
      
      static public void Setup()
      {
         list = new List<Equipment>();
         
         try
         {
            SQLiteConnection conn = new SQLiteConnection(@"Data Source=C:\Users\toadzky\Documents\Visual Studio 2010\Projects\APX_2010\2010.apx;");
            SQLiteCommand cmd = new SQLiteCommand("SELECT * FROM Equipment;", conn);
            conn.Open();
                     
            SQLiteDataReader rows = cmd.ExecuteReader();

            //rows.Read();
            
            while (rows.Read())
            {
               Equipment e = new Equipment(rows);
               list.Add(e);
            }

            conn.Close();

            list.Sort();
         }
         catch (Exception e)
         {
            Console.Out.WriteLine(e.Message);
         }
      }
      
      #endregion

      #region Retrieval Functions

      static public List<Equipment> List { get { if (list == null) Setup(); return list; } }

      /// <summary>
      /// Retreives the number of points a given sensor is worth.
      /// </summary>
      /// <param name="part">Part number for sensor</param>
      /// <returns># of points</returns>
      static public Double GetPoints(String part)
      {
         if (list == null)
            Setup();
         int i = (list.FindIndex (delegate(Equipment x) { return x.PartNumber == part; }) );
         
         if (i != -1)
            return list[i].PointValue;
         else return -1;
      }

      /// <summary>
      /// Retreives a given sensor's info from the database.
      /// </summary>
      /// <param name="part">Part number for sensor</param>
      /// <returns>The sensor</returns>
      static public Equipment GetEquipment(String part)
      {
         if (list == null)
            Setup();

         int i = (list.FindIndex(delegate(Equipment x) { return x.PartNumber == part; }));
         
         if (i != -1)
            return list[i];
         else return null;
      }

      #endregion
   }
}
